package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.AlarmDeviceRulesDTO;
import com.iwomy.secureplat.platform.entity.AlarmDeviceRulesEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.service.AlarmDeviceRulesService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 报警规则与设备管理表
 *
 * @author pig
 * @date 2024-02-19 14:22:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmDeviceRules")
@Tag(description = "alarmDeviceRules", name = "报警规则与设备管理表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmDeviceRulesController {

	private final AlarmDeviceRulesService alarmDeviceRulesService;
	private final BasicDeviceService basicDeviceService;

	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param alarmDeviceRules 报警规则与设备管理表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_view')")
	public R getAlarmDeviceRulesPage(@ParameterObject Page page, @ParameterObject AlarmDeviceRulesEntity alarmDeviceRules) {
		LambdaQueryWrapper<AlarmDeviceRulesEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(alarmDeviceRulesService.page(page, wrapper));
	}
	/**
	 * 查询
	 *
	 * @param alarmDeviceRules 报警规则与设备管理表
	 * @return
	 */
	@Operation(summary = "查询", description = "查询")
	@GetMapping("/list")
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_view')")
	public R getAlarmDeviceRulesList(   @ParameterObject AlarmDeviceRulesEntity alarmDeviceRules) {
		LambdaQueryWrapper<BasicDeviceEntity> deviceWrapper = Wrappers.lambdaQuery();
		deviceWrapper.eq(ObjectUtil.isNotEmpty(alarmDeviceRules.getDeviceModelId()),BasicDeviceEntity::getDeviceModelId,alarmDeviceRules.getDeviceModelId());
		deviceWrapper.eq(ObjectUtil.isNotEmpty(alarmDeviceRules.getDeviceTypeId()),BasicDeviceEntity::getDeviceTypeId,alarmDeviceRules.getDeviceTypeId());
		List<BasicDeviceEntity> basicDeviceEntities = basicDeviceService.listByScope(deviceWrapper, new DataScope());
		if(ObjectUtil.isEmpty(basicDeviceEntities)){
			return R.ok();
		}
		LambdaQueryWrapper<AlarmDeviceRulesEntity> wrapper = Wrappers.lambdaQuery(alarmDeviceRules);
		wrapper.in(AlarmDeviceRulesEntity::getImei,basicDeviceEntities.stream().map(BasicDeviceEntity::getImei).collect(Collectors.toList()));
		return R.ok(alarmDeviceRulesService.list( wrapper));
	}


	/**
	 * 通过id查询报警规则与设备管理表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmDeviceRulesService.getById(id));
	}

	/**
	 * 新增报警规则与设备管理表
	 *
	 * @param alarmDeviceRulesDTO 报警规则与设备管理表
	 * @return R
	 */
	@Operation(summary = "新增报警规则与设备管理表", description = "新增报警规则与设备管理表")
	@SysLog("新增报警规则与设备管理表")
	@PostMapping
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_add')")
	public R save(@RequestBody AlarmDeviceRulesDTO alarmDeviceRulesDTO) {
		alarmDeviceRulesService.addDeviceRulesByRules(alarmDeviceRulesDTO.getAlarmRulesId(),
				alarmDeviceRulesDTO.getDeviceList());
		return R.ok();
	}

	/**
	 * 修改报警规则与设备管理表
	 *
	 * @param alarmDeviceRules 报警规则与设备管理表
	 * @return R
	 */
	@Operation(summary = "修改报警规则与设备管理表", description = "修改报警规则与设备管理表")
	@SysLog("修改报警规则与设备管理表")
	@PutMapping
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_edit')")
	public R updateById(@RequestBody AlarmDeviceRulesEntity alarmDeviceRules) {
		return R.ok(alarmDeviceRulesService.updateById(alarmDeviceRules));
	}

	/**
	 * 通过id删除报警规则与设备管理表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警规则与设备管理表", description = "通过id删除报警规则与设备管理表")
	@SysLog("通过id删除报警规则与设备管理表")
	@DeleteMapping
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmDeviceRulesService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmDeviceRules 查询条件
	 * @param ids              导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
//	@PreAuthorize("@pms.hasPermission('platform_alarmDeviceRules_export')")
	public List<AlarmDeviceRulesEntity> export(AlarmDeviceRulesEntity alarmDeviceRules, Long[] ids) {
		return alarmDeviceRulesService.list(Wrappers.lambdaQuery(alarmDeviceRules).in(ArrayUtil.isNotEmpty(ids), AlarmDeviceRulesEntity::getId, ids));
	}
}